package abstraction;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;

import utils.ImageUtils;

/**
 * 
 * @author Maxime
 */
public abstract class ImageOperationDialog extends javax.swing.JDialog {

	/**
	 * A return status code - returned if Cancel button has been pressed
	 */
	public static final int RET_CANCEL = 0;
	/**
	 * A return status code - returned if OK button has been pressed
	 */
	public static final int RET_OK = 1;

	private javax.swing.JSeparator jSeparator2;
	protected javax.swing.JButton m_cancelButton;
	protected javax.swing.JPanel m_componentPanel;
	protected javax.swing.JLabel m_imageLabel;
	protected javax.swing.JButton m_okButton;
	protected javax.swing.JLabel m_titleLabel;
	// End of variables declaration//GEN-END:variables
	private int returnStatus = RET_CANCEL;
	protected int m_newWidth;
	protected int m_newHeight;
	protected BufferedImage m_current;
	protected BufferedImage m_preview;

	public BufferedImage getTransformedImage() {
		return m_preview;
	}

	/**
	 * Creates new form ImageOperationDialog
	 */
	public ImageOperationDialog(java.awt.Frame parent, BufferedImage source) {
		super(parent, true);

		initComponents();
		m_current = ImageUtils.deepCopy(source);
		m_preview = ImageUtils.deepCopy(m_current);
		int height = m_preview.getHeight();
		int width = m_preview.getWidth();
		m_newWidth = 300;
		m_newHeight = (height * m_newWidth) / width;
		m_imageLabel.setSize(m_newWidth, m_newHeight);
		m_imageLabel.setIcon(new ImageIcon(ImageUtils.resizeTo(m_preview,
				m_newWidth, m_newHeight)));
		pack();
		this.setLocationRelativeTo(parent);
		// Close the dialog when Esc is pressed
		String cancelName = "cancel";
		InputMap inputMap = getRootPane().getInputMap(
				JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
		ActionMap actionMap = getRootPane().getActionMap();
		actionMap.put(cancelName, new AbstractAction() {

			public void actionPerformed(ActionEvent e) {
				doClose(RET_CANCEL);
			}
		});
	}

	/**
	 * @return the return status of this dialog - one of RET_OK or RET_CANCEL
	 */
	public int getReturnStatus() {
		return returnStatus;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		m_titleLabel = new javax.swing.JLabel();
		javax.swing.JSeparator jSeparator1 = new javax.swing.JSeparator();
		m_componentPanel = new javax.swing.JPanel();
		javax.swing.JPanel jPanel3 = new javax.swing.JPanel();
		m_okButton = new javax.swing.JButton();
		m_cancelButton = new javax.swing.JButton();
		m_imageLabel = new javax.swing.JLabel();
		jSeparator2 = new javax.swing.JSeparator();

		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				closeDialog(evt);
			}
		});

		m_titleLabel.setText("Image tranformation");

		javax.swing.GroupLayout m_componentPanelLayout = new javax.swing.GroupLayout(
				m_componentPanel);
		m_componentPanel.setLayout(m_componentPanelLayout);
		m_componentPanelLayout.setHorizontalGroup(m_componentPanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 0, Short.MAX_VALUE));
		m_componentPanelLayout.setVerticalGroup(m_componentPanelLayout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 22, Short.MAX_VALUE));

		m_okButton.setText("OK");
		m_okButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				m_okButtonActionPerformed(evt);
			}
		});

		m_cancelButton.setText("Cancel");
		m_cancelButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				m_cancelButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(
				jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout
				.setHorizontalGroup(jPanel3Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel3Layout
										.createSequentialGroup()
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(
												m_okButton,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												67,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(m_cancelButton)));

		jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
				new java.awt.Component[] { m_cancelButton, m_okButton });

		jPanel3Layout
				.setVerticalGroup(jPanel3Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel3Layout
										.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(m_okButton)
										.addComponent(m_cancelButton)));

		getRootPane().setDefaultButton(m_okButton);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														m_componentPanel,
														javax.swing.GroupLayout.Alignment.TRAILING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(jSeparator1)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		m_titleLabel)
																.addGap(0,
																		0,
																		Short.MAX_VALUE))
												.addComponent(jSeparator2)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addGap(0,
																		129,
																		Short.MAX_VALUE)
																.addComponent(
																		jPanel3,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addComponent(
														m_imageLabel,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(m_titleLabel)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jSeparator1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										10,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(m_componentPanel,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addGap(4, 4, 4)
								.addComponent(jSeparator2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										10,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(4, 4, 4)
								.addComponent(m_imageLabel,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										205,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jPanel3,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(35, 35, 35)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void m_okButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_m_okButtonActionPerformed
		doClose(RET_OK);
	}// GEN-LAST:event_m_okButtonActionPerformed

	private void m_cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_m_cancelButtonActionPerformed
		doClose(RET_CANCEL);
	}// GEN-LAST:event_m_cancelButtonActionPerformed

	/**
	 * Closes the dialog
	 */
	private void closeDialog(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_closeDialog
		doClose(RET_CANCEL);
	}// GEN-LAST:event_closeDialog

	private void doClose(int retStatus) {
		returnStatus = retStatus;
		setVisible(false);
		dispose();
		// TODO affecter img ( deepcopy de preview)
	}

}
